package cn.org.timespace.easyframe.util.pager;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Pager
{
    /**
     * 当前页码
     */
    private int currentPageNum;
    
    /**
     * 每页展示的数量
     */
    private int itemPerPage =10;
    
    /**
     * 数据的总数量
     */
    private int totalCount;
    
    /**
     * 当前的数据
     */
    private List Data;
    
    /**
     * 地址
     */
    private String baseUrl;
    
    /**
     * 展示的页码数量
     */
    private int displayPageNum = 10;
    
    /**
     * 开始和结束的边界页码数量
     */
    private int edgePageNum = 2;
    
    
    /**
     * 查询条件
     */
    private Map<String,String> condition = new HashMap<String, String>();
    
    
    public int getFirstResult()
    {
        return itemPerPage * (currentPageNum - 1);
    }
    
    public void addParam(String name, String value)
    {
        condition.put(name, value);
    }
    
    public void addParams(Map<String ,String> map)
    {
        condition.putAll(map);
    }
    
    public void removeParam(String key)
    {
        condition.remove(key);
    }
    
    public Map<String, String> getCondition()
    {
        return condition;
    }

    public void setCondition(Map<String, String> condition)
    {
        this.condition = condition;
    }

    public int getCurrentPageNum()
    {
        return currentPageNum;
    }
    
    public boolean getHasPrePageNum()
    {
        return currentPageNum > 1 ;
        
    }
    
    public boolean getHasNextPageNum()
    {
        return currentPageNum<getTotalPageNum();
    }

    public void setCurrentPageNum(int currentPageNum)
    {
        this.currentPageNum = currentPageNum;
    }

    public List getData()
    {
        return Data;
    }

    public void setData(List data)
    {
        Data = data;
    }


    public int getItemPerPage()
    {
        return itemPerPage;
    }

    public void setItemPerPage(int itemPerPage)
    {
        this.itemPerPage = itemPerPage;
    }

    public int getTotalCount()
    {
        return totalCount;
    }

    public void setTotalCount(int totalCount)
    {
        this.totalCount = totalCount;
    }

    public int getTotalPageNum()
    {
        return (int)Math.ceil((double)totalCount/(double)itemPerPage);
    }
    
    public String getBaseUrl()
    {
        return baseUrl;
    }
    
    public void setBaseUrl(String baseUrl)
    {
        this.baseUrl = baseUrl;
    }
    
    public String getRequestUrl()
    {
        String url = baseUrl + "?";
        boolean first = true;
        for(String key : condition.keySet())
        {
            try
            {
                if(first)
                {
                    url = url + key + "=" + URLEncoder.encode(condition.get(key), "utf-8");
                    first = false;
                }
                else
                {
                    url = url + "&" + key + "=" + URLEncoder.encode(condition.get(key), "utf-8");
                }
                
            } catch (UnsupportedEncodingException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return url;
    }


    public int getDisplayPageNum()
    {
        return displayPageNum;
    }

    public void setDisplayPageNum(int displayPageNum)
    {
        this.displayPageNum = displayPageNum;
    }

    public int getEdgePageNum()
    {
        return edgePageNum;
    }

    public void setEdgePageNum(int edgePageNum)
    {
        this.edgePageNum = edgePageNum;
    }

   

    /**
     * 取得中间的页码
     * @return
     */
    public List<Integer> getInterval()
    {
        List<Integer> list = new ArrayList<Integer>();
        int half = (int)Math.ceil((double)displayPageNum/2);
        int np = getTotalPageNum();
        int upperLimit = np - displayPageNum;
        
        int start = currentPageNum > half ? Math.max(Math.min(currentPageNum-half, upperLimit), 1):1;
        int end = currentPageNum >  half ? Math.min(currentPageNum + half, np) : Math.min(displayPageNum, np);
        
        list.add(start);
        list.add(end);
        return list;
    }
    
    /**
     * 取得开始的页码
     * @return
     */
    public List<Integer> getStartPoints()
    {
        List<Integer> list = new ArrayList<Integer>();
        List<Integer> interval = getInterval();
        if(interval.get(0) > 1 && edgePageNum > 0)
        {
            int end = Math.min(interval.get(0), edgePageNum+1);
            for(int i=1; i<end; i++)
            {
                list.add(i);
            }
        }
        return list;
    }
    
    /**
     * 取得结束的页码
     * @return
     */
    public List<Integer> getEndPoints()
    {
        List<Integer> list = new ArrayList<Integer>();
        List<Integer> interval = getInterval();
        int tatalPageNum = getTotalPageNum();
        if (interval.get(1) < tatalPageNum && edgePageNum > 0)
        {
            int start = Math.max(tatalPageNum-edgePageNum+1, interval.get(1)+1);
            
            for(int i=start; i<=tatalPageNum; i++) 
            {
                list.add(i);
            }
        }

        return list;
    }
    
    
     
}
